package org.jetlinks.pro.assets.impl.web;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
import org.hswebframework.web.bean.FastBeanCopier;
import org.jetlinks.pro.assets.AssetBinding;
import org.jetlinks.pro.assets.AssetPermission;
import org.jetlinks.pro.assets.AssetType;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Getter
@Setter
public class AssetBindingView {

    @Schema(description = "绑定目标类型,例如:org")
    private String targetType;

    @Schema(description = "绑定目标的ID")
    private String targetId;

    @Schema(description = "资产类型,例如: device")
    private String assetType;

    @Schema(description = "资产ID")
    private String assetId;

    @Schema(description = "与资产的关系")
    private String relation;

    @Schema(description = "已经赋予的权限")
    private Set<String> grantedPermissions = new HashSet<>();

    @Schema(description = "全部可赋予的权限")
    private Set<String> allPermissions = new HashSet<>();

    public AssetBindingView merge(AssetBindingView view) {
        AssetBindingView newView = FastBeanCopier.copy(this, new AssetBindingView());

        newView.grantedPermissions.addAll(view.getGrantedPermissions());
        newView.allPermissions = view.getAllPermissions();

        return newView;
    }

    public static AssetBindingView of(AssetType type,
                                      String targetType,
                                      String targetId,
                                      String assetId) {
        AssetBindingView view = new AssetBindingView();
        view.setAssetType(type.getId());
        view.setAssetId(assetId);
        view.setTargetType(targetType);
        view.setTargetId(targetId);
        view.setAllPermissions(type
                                   .getPermissions()
                                   .stream()
                                   .map(AssetPermission::getValue)
                                   .collect(Collectors.toSet()));
        return view;
    }

    public static List<AssetBindingView> of(AssetType type,
                                            String targetType,
                                            String targetId,
                                            Collection<String> assetIdList) {
        return assetIdList
            .stream()
            .map(id -> of(type, targetType, targetId, id))
            .collect(Collectors.toList());
    }

    public static AssetBindingView of(AssetBinding binding) {
        return of(binding, false);
    }

    public static AssetBindingView ofGrantedIsAll(AssetBinding binding) {
        return of(binding, true);
    }

    public static AssetBindingView of(AssetBinding binding, boolean grantedIsAll) {
        AssetBindingView view = new AssetBindingView();
        view.setAssetId(binding.getAssetId());
        view.setAssetType(binding.getAssetType().getId());
        view.setTargetId(binding.getTargetId());
        view.setTargetType(binding.getTargetType().getId());
        view.setRelation(binding.getRelation());
        view.setGrantedPermissions(binding
                                       .getPermissions()
                                       .stream()
                                       .map(AssetPermission::getValue)
                                       .collect(Collectors.toSet()));

        view.setAllPermissions(grantedIsAll ?
                                   view.getGrantedPermissions()
                                   :
                                   binding
                                       .getAssetType()
                                       .getPermissions()
                                       .stream()
                                       .map(AssetPermission::getValue)
                                       .collect(Collectors.toSet()));
        return view;
    }
}
